/*  SSObjectFilterController.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Wed Aug 20 2003.
//
//SSObjectFilterController holds an arry of SSObjectFilter's, it can be passed an object and will determine
//if that object meets the requirments of all held filters.


#import "SSObjectFilterController.h"


@implementation SSObjectFilterController

- (id)init
{
   if (self = [super init]) {
      filterArray = [[NSMutableArray alloc] init];
   }
   return self;
}

- (BOOL)isFiltering
{
   return [filterArray count] != 0;
}

- (void)addFilterWithIdentifier:(int)identifier type:(int)type value:(id)value
{
   SSObjectFilter *newFilter;
   [self removeFilterWithIdentifier:identifier];

   newFilter = [[[SSObjectFilter alloc] initWithIdentifier:identifier type:type value:value] autorelease];
   [filterArray addObject:newFilter];
}

- (void)removeFilterWithIdentifier:(int)identifier
{
   NSEnumerator *enumerator = [[NSArray arrayWithArray:filterArray] objectEnumerator];
   id anObject;

   while (anObject = [enumerator nextObject]) { //remove any filters with the specified identifier
      if([anObject filterIdentifier] == identifier) {
         [filterArray removeObject:anObject];
      }
   }
}

- (SSObjectFilter *)filterWithIdentifier:(int)identifier
{
   NSEnumerator *enumerator = [filterArray objectEnumerator];
   id anObject;
   while (anObject = [enumerator nextObject]) {
      if([anObject filterIdentifier] == identifier) {
         return anObject;
      }
   }

   return nil;
}

- (BOOL)passesFilter:(id)object
{
   NSEnumerator *enumerator = [filterArray objectEnumerator];
   SSObjectFilter *filter;

   while (filter = [enumerator nextObject]) { //loop through the filters and determine if the object passes. if not return NO
      if(![filter passesFilter:object]) {
         return NO;
      }
   }

   return YES; //this object passes all filters
}
   
- (void)dealloc
{
   [filterArray release];
   [super dealloc];
}

@end
